package com.sskj.award.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.award.R;
import com.sskj.award.R2;
import com.sskj.award.bean.PlatformDataBean;
import com.sskj.award.component.DaggerUserDataComponent;
import com.sskj.award.presenter.SuperPlatformPrestener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.NumberUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @ClassName: SuperPlatformActivity
 * @Description: 类作用描述: 超级平台
 * @Author: yuanlin
 * @Date: 2019/7/11 15:27
 */

@Route(path = RConfig.SUPER_PLATFORM)
public class SuperPlatformActivity extends BaseActivity<SuperPlatformPrestener> {

    @BindView(R2.id.tvShowCardNum)
    TextView tvShowCardNum;
    @BindView(R2.id.tvShowDongJieMoney)
    TextView tvShowDongJieMoney;
    @BindView(R2.id.tvShowShiFangMoney)
    TextView tvShowShiFangMoney;
    @BindView(R2.id.tvShowJiaSuSpeed)
    TextView tvShowJiaSuSpeed;
    @BindView(R2.id.tvShowZhiTuiCount)
    TextView tvShowZhiTuiCount;
    @BindView(R2.id.tvShowZhiTuiPlatformCount)
    TextView tvShowZhiTuiPlatformCount;
    @BindView(R2.id.tvShowZhiTuiTotalCount)
    TextView tvShowZhiTuiTotalCount;
    @BindView(R2.id.tvShowTeamCount)
    TextView tvShowTeamCount;
    @BindView(R2.id.tvShowTeamSuperPlatformCount)
    TextView tvShowTeamSuperPlatformCount;
    @BindView(R2.id.tvShowTeamTotalCount)
    TextView tvShowTeamTotalCount;
    @BindView(R2.id.llBtLeft)
    LinearLayout llBtLeft;
    @BindView(R2.id.tvLeft)
    TextView tvLeft;
    @BindView(R2.id.viewSplit1)
    View viewSplit1;
    @BindView(R2.id.llBtRight)
    LinearLayout llBtRight;
    @BindView(R2.id.tvRightTitle)
    TextView tvRightTitle;
    @BindView(R2.id.viewSplit2)
    View viewSplit2;
    @BindView(R2.id.tvDescribe1)
    TextView tvDescribe1;
    @BindView(R2.id.tvDescribe2)
    TextView tvDescribe2;
    @BindView(R2.id.tvDescribe3)
    TextView tvDescribe3;
    @BindView(R2.id.coolRefreshView)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.btBuy)
    Button btBuy;
    private AlertDialog.Builder builder;
    private AlertDialog buyDialog;

    private BaseAdapter<PlatformDataBean.DynamicLogsBean.DataBeanX> logAdapter;
    private BaseAdapter<PlatformDataBean.ReturnLogsBean.DataBean> usdtAdapter;
    private List<PlatformDataBean.DynamicLogsBean.DataBeanX> dynamic_logs = new ArrayList<>();
    private List<PlatformDataBean.ReturnLogsBean.DataBean> returnLogsBeans = new ArrayList<>();

    private int page = 1;
    private int type = 1;//底部列表状态
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    @Autowired
    boolean isSuper;
    private boolean flag=false;

    @Override
    protected int getLayoutId() {
        return R.layout.super_layout_activity_super_platform;
    }

    @Override
    public SuperPlatformPrestener getPresenter() {
        return new SuperPlatformPrestener();
    }


    @Override
    protected void initView() {
        super.initView();
        setTitle(App.INSTANCE.getString(R.string.super_platform));
        ARouter.getInstance().inject(this);
        flag=isSuper;

        DaggerUserDataComponent.create().inject(this);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                returnLogsBeans.clear();
                dynamic_logs.clear();
                if (type == 1) {
                    mPresenter.getPlatData(String.valueOf(page));
                } else {
                    mPresenter.getPlatData(String.valueOf(page));
                }
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                if (type == 1) {
                    mPresenter.getPlatData(String.valueOf(page));
                } else {
                    mPresenter.getPlatData(String.valueOf(page));
                }
            }
        });

        ClickUtil.click(llBtLeft, () -> {
            tvLeft.setTextColor(getResources().getColor(R.color.super_text_blue));
            viewSplit1.setVisibility(View.VISIBLE);
            tvRightTitle.setTextColor(getResources().getColor(R.color.super_8e9));
            viewSplit2.setVisibility(View.INVISIBLE);
            tvDescribe1.setText(App.INSTANCE.getString(R.string.super_shifang_money));
            tvDescribe2.setText(App.INSTANCE.getString(R.string.super_jiasu_release));
            page = 1;
            dynamic_logs.clear();
            type = 1;
            mPresenter.getPlatData(String.valueOf(page));
        });
        ClickUtil.click(llBtRight, () -> {
            tvRightTitle.setTextColor(getResources().getColor(R.color.super_text_blue));
            tvLeft.setTextColor(getResources().getColor(R.color.super_8e9));
            viewSplit1.setVisibility(View.INVISIBLE);
            viewSplit2.setVisibility(View.VISIBLE);
            tvDescribe1.setText(App.INSTANCE.getString(R.string.super_fenhong_money));
            tvDescribe2.setText(App.INSTANCE.getString(R.string.super_leixing));
            returnLogsBeans.clear();
            page = 1;
            type = 2;
            mPresenter.getPlatData(String.valueOf(page));
        });
        //购买弹窗
        ClickUtil.click(btBuy, new ClickUtil.Click() {
            @Override
            public void click() {
                showBuyDialog();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.super_divi_split))
        );
    }


    @Override
    protected void initData() {
        super.initData();
        mPresenter.getPlatData(String.valueOf(page));
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (userData.getSuper_user() == 1) {
                    btBuy.setVisibility(View.GONE);
                }
            } else {
                userData = null;
            }
            if (flag){
                showBuyDialog();
            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }


    @SuppressLint("SetTextI18n")
    private void showBuyDialog() {
        if (userData == null) {
            return;
        }
        if (buyDialog == null) {
            builder = new AlertDialog.Builder(this);
            View view = LayoutInflater.from(this).inflate(R.layout.super_buy_dialog_next, null, false);
            builder.setView(view);
            buyDialog = builder.create();
            buyDialog.setCanceledOnTouchOutside(false);
            TextView etBuyMoney = view.findViewById(R.id.superDialogMoney);

            etBuyMoney.setText(userData.getUpgradeSuperUserMoney() + "USDT");
            EditText etBuyPwd = view.findViewById(R.id.etBuyPwd);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            ClickUtil.click(tvCancel, () -> {
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });

            ClickUtil.click(tvConfirm, () -> {
                String buyPwd = etBuyPwd.getText().toString().trim();
                if (TextUtils.isEmpty(buyPwd)) {
                    Toast.makeText(mActivity, App.INSTANCE.getString(R.string.super_buy_hint_pwd), Toast.LENGTH_SHORT).show();
                    return;
                }
                // TODO: 2019/7/12 调用接口
                mPresenter.upDataSuperUser(etBuyPwd.getText().toString());
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });
            Window window = buyDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        }
        buyDialog.show();

    }

    @SuppressLint("SetTextI18n")
    public void getPlatDataSuccess(PlatformDataBean data) {
        if (data == null) {
            return;
        }
        if (type == 1) {
            List<PlatformDataBean.ReturnLogsBean.DataBean> data1 = data.getReturn_logs().getData();
            returnLogsBeans.addAll(data1);
            usdtAdapter = new BaseAdapter<PlatformDataBean.ReturnLogsBean.DataBean>(R.layout.super_platform_item_record, null, recyclerView, true) {
                @Override
                public void bind(ViewHolder holder, PlatformDataBean.ReturnLogsBean.DataBean item) {
                    holder.setText(R.id.tvDescribe1, NumberUtil.keepNoZero(item.getMoney()) + "USDT")
                            .setText(R.id.tvDescribe2, NumberUtil.keepNoZero(item.getMoney()) + App.INSTANCE.getString(R.string.super_superPlatformActivity2))
                            .setText(R.id.tvDescribe3, item.getCreated_at() == null ? "" : item.getCreated_at())
                    ;
                }
            };
            usdtAdapter.addData(returnLogsBeans);
        } else if (type == 2) {
            dynamic_logs.addAll(data.getDynamic_logs().getData());
            logAdapter = new BaseAdapter<PlatformDataBean.DynamicLogsBean.DataBeanX>(R.layout.super_platform_item_record, null, recyclerView, true) {
                @Override
                public void bind(ViewHolder holder, PlatformDataBean.DynamicLogsBean.DataBeanX item) {
                    holder.setText(R.id.tvDescribe1, NumberUtil.keepNoZero(item.getMoney()) + "USDT")
                            .setText(R.id.tvDescribe3, item.getCreated_at())
                    ;
                    switch (item.getType()) {
                        case "1":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity3));
                            break;
                        case "2":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity4));
                            break;
                        case "3":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity5));
                            break;
                        case "4":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity6));
                            break;
                        case "5":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity7));
                            break;
                        case "6":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity8));
                            break;
                        case "7":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity9));
                            break;
                        case "8":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity10));
                            break;
                        case "9":
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity11));
                            break;
                        default:
                            holder.setText(R.id.tvDescribe2, App.INSTANCE.getString(R.string.super_superPlatformActivity12));
                            break;
                    }
                }
            };
            logAdapter.addData(dynamic_logs);
        }

        tvShowCardNum.setText(String.valueOf(data.getUsdt_return().getId()));
        tvShowDongJieMoney.setText(NumberUtil.keepNoZero(data.getUsdt_return().getLocked()) + "USDT");
        tvShowShiFangMoney.setText(NumberUtil.keepNoZero(data.getUsdt_return().getAlready_return()) + "USDT");
        tvShowJiaSuSpeed.setText(NumberUtil.keepNoZero(data.getUsdt_return().getAcceleration_rate()) + App.INSTANCE.getString(R.string.super_superPlatformActivity13));
        tvShowZhiTuiCount.setText(data.getTui_count() + App.INSTANCE.getString(R.string.super_airDropActivity2));
        tvShowZhiTuiPlatformCount.setText(data.getTui_super() + App.INSTANCE.getString(R.string.super_airDropActivity2));
        tvShowZhiTuiTotalCount.setText(NumberUtil.keepNoZero(NumberUtil.keep2(data.getTui_yeji())) + "USDT");
        tvShowTeamCount.setText(data.getTeam_count() + App.INSTANCE.getString(R.string.super_airDropActivity2));
        tvShowTeamSuperPlatformCount.setText(data.getTeam_super() + App.INSTANCE.getString(R.string.super_airDropActivity2));
        tvShowTeamTotalCount.setText(NumberUtil.keepNoZero(NumberUtil.keep2(data.getTeam_yeji())) + "USDT");
    }

    public void upDataSuperUserSuccess() {
        btBuy.setVisibility(View.GONE);
        userViewModel.update();
        ToastUtil.showShort(App.INSTANCE.getString(R.string.super_superPlatformActivity15));
    }

    public void onRefreshStop() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadMore();
            smartRefreshLayout.finishRefresh();
        }
    }
}
